package com.tsd.base.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.tsd.base.entity.BaseExchangeRate;
import com.tsd.base.entity.BaseExchangeRateExt;
import com.tsd.base.service.BaseExchangeRateService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.controller.SystemBaseController;
import com.tsd.system.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：BaseExchangeRatebase_exchange_rate 控制层
 *
 * @author zsy
 * @date 2020/12/29 15:22:59
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "base_exchange_rate-base_exchange_rate", description = "base_exchange_rate的API描述")
public class BaseExchangeRateController extends SystemBaseController {

    @Resource
    private BaseExchangeRateService baseExchangeRateService;

    @ApiOperation(value = "根据id获取base_exchange_rate", notes = "根据id获取base_exchange_rate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "base_exchange_rateid", required = true, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getBaseExchangeRateById.action", method = RequestMethod.POST)
    public HttpResult<BaseExchangeRate> getBaseExchangeRateById() {
        HttpResult<BaseExchangeRate> hr = new HttpResult<BaseExchangeRate>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            BaseExchangeRate baseExchangeRate = baseExchangeRateService.getBaseExchangeRateById(id);
            hr.setData(baseExchangeRate);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "获取当前默认的汇率", notes = "获取当前默认的汇率")
    @ResponseBody
    @RequestMapping(value = "/getDefaultBaseExchangeRate.action", method = RequestMethod.POST)
    public HttpResult<BaseExchangeRate> getDefaultBaseExchangeRate() {
        HttpResult<BaseExchangeRate> hr = new HttpResult<BaseExchangeRate>();
        try {
            Map<String, Object> map = super.getParams(request);
            BaseExchangeRate baseExchangeRate = baseExchangeRateService.getDefaultBaseExchangeRate();
            hr.setData(baseExchangeRate);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除base_exchange_rate", notes = "根据id删除base_exchange_rate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "base_exchange_rateid", required = true, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteBaseExchangeRate.action", method = RequestMethod.POST)
    public HttpResult<String> deleteBaseExchangeRate() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            baseExchangeRateService.deleteBaseExchangeRate(id, null);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据ids批量删除base_exchange_rate", notes = "根据ids批量删除base_exchange_rate")
    @ResponseBody
    @RequestMapping(value = "/batchDelBaseExchangeRate.action", method = RequestMethod.POST)
    public HttpResult<String> batchDelBaseExchangeRate() throws HlpException {
        HttpResult<String> hr = new HttpResult<String>();
        SysUser opUser = super.getSysUser();
        try {
            Map<String, Object> map = super.getParams(request);
            map = super.getParams(request);
            Object obj = map.get("ids");
            super.checkEmpty(obj, "请提供参数ids");
            if (obj instanceof List) {
                List<Long> ids = JSONArray.parseArray(JSON.toJSONString(obj), Long.class);
                if (ids.size() == 0) throw new HlpException("请至少选择一项");
                baseExchangeRateService.batchDelBaseExchangeRate(ids, opUser);
                hr.setCode(ErrorCode.CODE_SUCCESS);
            } else {
                throw new HlpException("数据类型不正确，非数组类型");
            }
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存base_exchange_rate", notes = "保存base_exchange_rate，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "base_exchange_rate", required = true, paramType = "body", dataType = "BaseExchangeRate")
    })
    @ResponseBody
    @RequestMapping(value = "/saveBaseExchangeRate.action", method = RequestMethod.POST)
    public HttpResult<String> saveBaseExchangeRate() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            SysUser opUser = super.getSysUser();
            String json = JSON.toJSONString(map);
            BaseExchangeRate t = JSON.parseObject(json, BaseExchangeRate.class);
            baseExchangeRateService.saveBaseExchangeRate(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "批量保存base_exchange_rate", notes = "批量保存base_exchange_rate，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "base_exchange_rate", required = true, paramType = "body", dataType = "BaseExchangeRate")
    })
    @ResponseBody
    @RequestMapping(value = "/batchSaveBaseExchangeRate.action", method = RequestMethod.POST)
    public HttpResult<String> batchSaveBaseExchangeRate() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            SysUser opUser = super.getSysUser();
            String list = HlpUtils.getString(map, "list");
            List<BaseExchangeRateExt> t = JSON.parseArray(list, BaseExchangeRateExt.class);
            baseExchangeRateService.batchSaveBaseExchangeRate(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询base_exchange_rate", notes = "分页查询base_exchange_rate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findBaseExchangeRates.action", method = RequestMethod.POST)
    public HttpResult<List<BaseExchangeRateExt>> findBaseExchangeRates() {
        HttpResult<List<BaseExchangeRateExt>> hr = new HttpResult<List<BaseExchangeRateExt>>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<String, Object>();
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            String currency_type = HlpUtils.getString(map, "currency_type");
            if (!HlpUtils.isEmpty(currency_type)) {
                params.put("currency_type", "%" + currency_type + "%");
            }
            PageBean page = new PageBean(map);
            if (map.containsKey("nopage")) {
                page = null;
            }
            List<BaseExchangeRateExt> list = null;
            if (HlpUtils.isEmpty(page)) {
                list = baseExchangeRateService.findBaseExchangeRates(params);
            } else {
                list = baseExchangeRateService.findBaseExchangeRates(params, page);
            }
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
